import * as THREE from 'three';
import * as OIMO from 'oimo';
import {OrbitControls} from 'three/examples/jsm/controls/OrbitControls.js';


var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);


var axesHelper = new THREE.AxesHelper(100); // 参数表示轴的长度
scene.add(axesHelper);

const cube = new THREE.Mesh(
    new THREE.BoxGeometry(10, 10, 10),
    new THREE.MeshBasicMaterial({
        color: 0x607d8b,
    }),
)
cube.position.copy(new THREE.Vector3(0, 10, 0));
scene.add(cube)


var planeGeometry = new THREE.PlaneGeometry(100, 100, 1, 1);
var planeMaterial = new THREE.MeshBasicMaterial({color: 0xff0000, side: THREE.DoubleSide});
var plane = new THREE.Mesh(planeGeometry, planeMaterial);
console.log(plane.position)
plane.rotation.x = Math.PI / 2;
scene.add(plane)


const world = new OIMO.World({
    timestep: 1 / 60,
    iterations: 8,
    broadphase: 2,
    worldscale: 1,
    random: true,
    info: false,
});

const groundBody = world.add({
    type: 'box',
    size: [10000, 0.01, 10000],
    pos: [0, 0, 0],
    rot: [0, 0, 0],
    move: false, // 是否可移动
    density: 1, // 密度
    friction: 1, // 摩擦力
    restitution: 0.1, // 弹性
    belongsTo: 1,
});

const body = world.add({
    type: 'box',
    size: [10, 10, 10], // 半径
    pos: [0, 10, 0], // 位置
    rot: [0, 0, 0], // 旋转
    move: true, // 是否可移动
    density: 10, // 密度
    friction: 0.2, // 摩擦力
    restitution: 1, // 弹性
    belongsTo: 1,
    collidesWith: 0xffffffff, // 0xffffffff
});


const directionalLight = new THREE.DirectionalLight(0xffff00, 0.5);
scene.add(directionalLight);

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const controls = new OrbitControls(cube, renderer.domElement);

function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
    world.step();
    controls.update(); // 更新控制器状态
    cube.position.copy(body.getPosition());
    cube.quaternion.copy(body.getQuaternion());
}

camera.position.x = 0
camera.position.y = 0
camera.position.z = 100
animate();